﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    /// <summary>
    /// 输出一个InfoBox控件,参见AdminLte的infobox控件
    /// </summary>
    public class PageCommon_InfoBox:ControlBase
    {
        private ColorPaletteEnum _color = ColorPaletteEnum.info;
        private bool _withBackgroundColor = false;
        private bool _withBorder = false;
        private List<string> _lst=new List<string>();
        private string _iconCss = "fa-star-o";
        private bool _noIcon = false;
        private string _title = "";
        private string _href = "";
        private InfoBoxSizeEnum _size = InfoBoxSizeEnum.Large;

        public PageCommon_InfoBox(ViewContext viewContext) : base(viewContext)
        {
            this.Css("info-box webui-ctrl-infobox");
        }

        public PageCommon_InfoBox Href(string gotoUrl)
        {
            _href = gotoUrl;

            return this;
        }

        public PageCommon_InfoBox Color(ColorPaletteEnum color)
        {
            _color = color;
            return this;
        }

        /// <summary>
        /// infobox框图标的大小
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public PageCommon_InfoBox Size(InfoBoxSizeEnum size)
        {
            _size = size;
            return this;
        }

        /// <summary>
        /// infobox带背景色
        /// </summary>
        /// <returns></returns>
        public PageCommon_InfoBox WithBackgroundColor()
        {
            _withBackgroundColor = true;

            return this;
        }

        public PageCommon_InfoBox WithBorder(bool withBorder = true)
        {
            _withBorder = withBorder;

            return this;
        }

        public PageCommon_InfoBox NoIcon(bool noIcon = true)
        {
            _noIcon = noIcon;

            return this;
        }

        public PageCommon_InfoBox IconCss(string iconCss)
        {
            _iconCss = iconCss;
            return this;
        }

        /// <summary>
        /// 输出一个普通文本块
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageCommon_InfoBox AddText(string value, string css = "")
        {
            _lst.Add($"<span class=\"info-box-text {css}\">{value}</span>");

            return this;
        }

        /// <summary>
        /// 添加一个数字块
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public PageCommon_InfoBox AddNumber(string value,string css="")
        {
            _lst.Add($"<span class=\"info-box-number {css}\">{value}</span>");

            return this;
        }

        

        public override HtmlString Render()
        {
            if (_withBackgroundColor)
            {
                this.Css("bg-" + _color);
            }

            if (_withBorder)
            {
                this.Css("info-box-border");
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            if (_size!=InfoBoxSizeEnum.Large)
            {
                Attributes.Class(_size.If(x => x == InfoBoxSizeEnum.Middle, "middlesize")
                    .Elseif(x => x == InfoBoxSizeEnum.Small, "smallsize").Else("").Result);
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            var hasLink = !string.IsNullOrWhiteSpace(_href);

            if (hasLink)
            {
                ViewContext.Writer.WriteLine($"<a href=\"{_href}\">");
            }

            if (!_noIcon)
            {
                ViewContext.Writer.WriteLine($"<span class=\"info-box-icon {_withBackgroundColor.If("", "bg-" + _color)} \"><i class=\"fa {_iconCss}\"></i></span>");
            }

            ViewContext.Writer.WriteLine($"<div class=\"info-box-content\" style=\"{(_noIcon ? "margin-left:0;" : "")}\">");

            foreach (var item in _lst)
            {
                ViewContext.Writer.WriteLine(item);
            }

            ViewContext.Writer.WriteLine("</div>");

            if (hasLink)
            {
                ViewContext.Writer.WriteLine("</a>");
            }

            ViewContext.Writer.WriteLine("</div> <!-- .info-box -->");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }
    }
}
